/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * receiptReaderTool.java
 *
 * Created on 2011-05-22, 17:31:04
 */
package receiptReaderTool;

import Structures.ProfileDataBase;
import Structures.Entry;
import Structures.Entry_rawDecorator;
import Structures.ReceiptProfile;
import homebudget.DataBaseMySql;
import java.awt.Color;
import java.awt.Component;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import javax.swing.filechooser.FileFilter;
import homebudget.ExtensionFileFilter;
import homebudget.HomeBudgetApp;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import javax.swing.ComboBoxModel;

/**
 *
 * @author Mariusz
 */
public class receiptReaderTool extends javax.swing.JFrame {
    private static String DEFAULT_NULL_CATEGORY = "BRAK";
    private static String CONNECTION_STRING = HomeBudgetApp.getConnectionString();
    private DataBaseMySql sqldb;
    private HashMap<Integer, Boolean> recognized;
    private ProfileDataBase db;
    private int budgetID;

    /** Creates new form receiptReaderTool */
//    public receiptReaderTool(ProfileDataBase db) {
//        this.db = db;
//        initComponents();
//    }

    public receiptReaderTool(int budgetID) {
        this.sqldb = new DataBaseMySql(CONNECTION_STRING);
        this.db = new ProfileDataBase();
        this.budgetID = budgetID;
        
        // Categories initialization
        //LinkedList<String> tmp = sqldb.listaKategoriaProdukt(); 
        
        initComponents();       
        LinkedList<String> tmp = sqldb.listaKategoriaProdukt();
        ComboBoxModel model = new DefaultComboBoxModel(tmp.toArray());
        jComboBox2.setModel(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel11 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(homebudget.HomeBudgetApp.class).getContext().getResourceMap(receiptReaderTool.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Start"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setName("jLabel1"); // NOI18N

        jTextField1.setEditable(false);
        jTextField1.setName("jTextField1"); // NOI18N

        jLabel2.setName("jLabel2"); // NOI18N

        jComboBox1.setModel(new DefaultComboBoxModel(db.getProfilesArray()));
        jComboBox1.setName("jComboBox1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 315, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Wyjście z analizatora"));
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(250, 411));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setName("jList1"); // NOI18N
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Rozpoznane produkty"));
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(250, 411));
        jPanel3.setRequestFocusEnabled(false);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList2.setName("jList2"); // NOI18N
        jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList2ValueChanged(evt);
            }
        });
        jList2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jList2KeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(jList2);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel3.setName("jLabel3"); // NOI18N

        jTextField2.setName("jTextField2"); // NOI18N

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Produkt"));
        jPanel4.setName("jPanel4"); // NOI18N

        jLabel4.setName("jLabel4"); // NOI18N

        jLabel6.setName("jLabel6"); // NOI18N

        jTextField4.setName("jTextField4"); // NOI18N

        jTextField3.setName("jTextField3"); // NOI18N

        jLabel5.setName("jLabel5"); // NOI18N

        jTextField5.setName("jTextField5"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        jComboBox2.setName("jComboBox2"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7)
                    .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(112, 112, 112)))
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel6)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, 0, 187, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(resourceMap.getFont("jTextArea1.font")); // NOI18N
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setText(resourceMap.getString("jTextArea1.text")); // NOI18N
        jTextArea1.setWrapStyleWord(true);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane3.setViewportView(jTextArea1);

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jTextField6.setText(resourceMap.getString("jTextField6.text")); // NOI18N
        jTextField6.setName("jTextField6"); // NOI18N

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jTextField7.setEditable(false);
        jTextField7.setText(resourceMap.getString("jTextField7.text")); // NOI18N
        jTextField7.setName("jTextField7"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField7))
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(11, 11, 11)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser fc = new JFileChooser();
        FileFilter type = new ExtensionFileFilter("Tylko obrazki",
            new String[] { "jpg", "gif", "jpeg", "png", "bmp" });
        fc.setFileFilter(type);
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            jTextField1.setText(fc.getSelectedFile().getAbsolutePath());
        }
//        JOptionPane.showMessageDialog(null, "Nie dotykaj obrazów podstawowych profili.", "Błąd!", JOptionPane.ERROR_MESSAGE);
//        return;
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (jTextField1.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(null, "Wybierz plik wejściowy.", "Błąd!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        ReceiptProfile profile;
        String output;
        try{
            profile = db.profiles.get(jComboBox1.getSelectedIndex());
            output = profile.readReceipt(jTextField1.getText());
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, "Niepoprawny paragon lub obrazy treningowe.", "Błąd!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        LinkedList<String> lines = profile.parseString(output);
        jList1.setModel(new receiptReaderTool_jListRawStringModel(lines));
        
        LinkedList<Entry_rawDecorator> products;
        if(profile.isDefault()){
            recognized = new HashMap<Integer, Boolean>();
            products = new LinkedList<Entry_rawDecorator>();
            
            int i = 0;
            for(String s : lines){
                StringBuilder sb = new StringBuilder();
                if(lines.size() != i+1){
                    sb.append(s.replace("\r", ""));
                    sb.append(" ");
                    sb.append(lines.get(i+1));
                }
                if((profile.checkIfProduct(s) || 
                        (products.size() > 0 && 
                        profile.checkIfProduct(sb.toString()) && 
                        !profile.checkIfProduct(s) && 
                        !profile.checkIfProduct(lines.get(i+1)))) 
                        && !recognized.containsKey(i)){
                    try{
                        Entry temp = profile.getProduct(s);
                        temp.category = DEFAULT_NULL_CATEGORY;
                        temp.shop = (String) jComboBox1.getSelectedItem();
                        temp.date = new Date(0);
                        products.add(new Entry_rawDecorator(temp, new int[]{i}));
                        recognized.put(i, true);
                    }catch(Exception ex){
                        try{
                            String tempString = s.replace("\r", "") + " " + lines.get(i+1);
                            Entry temp = profile.getProduct(tempString);
                            temp.category = DEFAULT_NULL_CATEGORY;
                            temp.shop = (String) jComboBox1.getSelectedItem();
                            temp.date = new Date(0);
                            products.add(new Entry_rawDecorator(temp, new int[]{i, i+1}));
                            recognized.put(i, true);
                            recognized.put(i+1, true);
                        }catch(Exception e){
                            recognized.put(i, false);
                        }
                    }
                }                
                ++i;
            }
            
            jList1.setCellRenderer(new recognizedProductRenderer(jList1.getCellRenderer()));
            refreshTotal(products);
            
            jList2.setModel(new receiptReaderTool_jListProductModel(products));
        }
    }//GEN-LAST:event_jButton2ActionPerformed
   
    private void refreshTotal(LinkedList<Entry_rawDecorator> products){
        float total = 0;
        for(Entry_rawDecorator en : products)
            total += en.entry.price * en.entry.quantity;
        jTextField7.setText(Float.toString(total));
    }
    
    private class recognizedProductRenderer extends JLabel implements ListCellRenderer{
        private ListCellRenderer cr;
        
        public recognizedProductRenderer(ListCellRenderer cellRenderer) {
            cr = cellRenderer;
            setOpaque(true);
        }
        
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component comp = cr.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if(recognized != null && recognized.get(index) != null && recognized.get(index).booleanValue())
                comp.setBackground(Color.green);
            else
                comp.setBackground(new DefaultListCellRenderer().getListCellRendererComponent(list, value, index, isSelected, cellHasFocus).getBackground());
            
            return comp;
        }
       
    }
   
    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        StringBuilder sb = new StringBuilder();
        for(int i : jList1.getSelectedIndices())
            sb.append(jList1.getModel().getElementAt(i));
        
        jTextField2.setText(sb.toString());
    }//GEN-LAST:event_jList1ValueChanged

    private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList2ValueChanged
        if(jList2.getSelectedIndices().length == 0){
            jTextField3.setText("");
            jTextField4.setText("");
            jTextField5.setText("");
            return;
        }
        
        try{
            Entry_rawDecorator temp = (Entry_rawDecorator)jList2.getModel().getElementAt(jList2.getSelectedIndex());
            jList1.setSelectedIndices(temp.rawNumber);
            
            jTextField3.setText(temp.entry.name);
            jTextField4.setText(Float.toString(temp.entry.price));
            jTextField5.setText(Float.toString(temp.entry.quantity));
            jComboBox2.setSelectedItem(temp.entry.category);
        }catch(Exception ex){
        }
    }//GEN-LAST:event_jList2ValueChanged

    private void jList2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jList2KeyTyped
        if(evt.getKeyChar() == evt.VK_DELETE){
            try {
                Entry_rawDecorator deleted = (Entry_rawDecorator) ((receiptReaderTool_jListProductModel) jList2.getModel()).removeElementAt(jList2.getSelectedIndex());
                //((receiptReaderTool_jListProductModel) jList2.getModel()).prod.remove(jList2.getSelectedIndex());
                jList2.setModel(new receiptReaderTool_jListProductModel(((receiptReaderTool_jListProductModel) jList2.getModel()).prod));
                
                for(int i : deleted.rawNumber){
                    recognized.put(i, false);
                }
                jList1.setCellRenderer(new recognizedProductRenderer(jList1.getCellRenderer()));
                
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                jList2.setSelectedIndices(null);
                
                refreshTotal(((receiptReaderTool_jListProductModel)jList2.getModel()).prod);
            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_jList2KeyTyped

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if(jList1.getModel().getSize() == 0){
            JOptionPane.showMessageDialog(null, "Na początek wczytaj paragon.", "Błąd!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(jTextField3.getText().isEmpty() || 
                jTextField4.getText().isEmpty() || 
                jTextField5.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "Uzupełnij wszystkie pola.", "Błąd!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(!jList2.isSelectionEmpty()){
            try{
                Entry_rawDecorator orig = (Entry_rawDecorator) jList2.getSelectedValue();
                Entry temp = new Entry();
                temp.category = orig.entry.category;
                temp.date = orig.entry.date;
                temp.name = orig.entry.name;
                temp.price = orig.entry.price;
                temp.quantity = orig.entry.quantity;
                temp.shop = orig.entry.shop;
                
                
                temp.name = jTextField3.getText();
                try {
                    temp.price = Float.parseFloat(jTextField4.getText());
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "Niepoprawna cena.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                try {
                    temp.quantity = Float.parseFloat(jTextField5.getText());
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "Niepoprawna ilość.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                temp.category = (String) jComboBox2.getSelectedItem();
                
                orig.entry = temp;
                
                jList2.setModel(new receiptReaderTool_jListProductModel(((receiptReaderTool_jListProductModel)jList2.getModel()).prod));
                refreshTotal(((receiptReaderTool_jListProductModel)jList2.getModel()).prod);
                return;
            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, "Coś poszło nie tak przy edycji.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        
        if(!jList1.isSelectionEmpty() && jList2.isSelectionEmpty()){
            int[] selectedRows = jList1.getSelectedIndices();
            if(selectedRows.length > 2){
                JOptionPane.showMessageDialog(null, "Jeden produkt zajmuje maksymalnie dwie linie. Wybierz max 2 wiersze z listy po lewej.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            if(selectedRows.length == 2 && Math.abs(selectedRows[0]-selectedRows[1]) != 1){
                JOptionPane.showMessageDialog(null, "Jeden produkt może składać się z dwóch linii, ale nie mogą być one oddalone od siebie o wiecej niż 1 wiersz.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            for(int i : selectedRows){
                if(recognized.containsKey(i) && recognized.get(i)){
                    JOptionPane.showMessageDialog(null, "Jeden z wierszy został już rozpoznany jako produkt.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            
            
            try{
                LinkedList<Entry_rawDecorator> model = ((receiptReaderTool_jListProductModel)jList2.getModel()).prod;
                Entry_rawDecorator temp = new Entry_rawDecorator(new Entry(), selectedRows);
                temp.entry.category = DEFAULT_NULL_CATEGORY;
                temp.entry.shop = (String) jComboBox1.getSelectedItem();
                temp.entry.date = new Date(0);
                
                temp.entry.name = jTextField3.getText();
                try {
                    temp.entry.price = Float.parseFloat(jTextField4.getText());
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "Niepoprawna cena.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                try {
                    temp.entry.quantity = Float.parseFloat(jTextField5.getText());
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "Niepoprawna ilość lub kategoria.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                temp.entry.category = (String) jComboBox2.getSelectedItem();
                model.add(temp);
                
                jList2.setModel(new receiptReaderTool_jListProductModel(((receiptReaderTool_jListProductModel)jList2.getModel()).prod));
                refreshTotal(((receiptReaderTool_jListProductModel)jList2.getModel()).prod);
                
                for(int i : selectedRows)
                    recognized.put(i, true);
                jList1.setCellRenderer(new recognizedProductRenderer(jList1.getCellRenderer()));
                
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, "Coś poszło nie tak przy edycji.", "Błąd!", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }else{
            JOptionPane.showMessageDialog(null, "Przed dodaniem produktu wybierz wiersz go reprezentujący z listy po lewej (maksymalnie 2 wiersze).", "Błąd!", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        // - format daty nie został rozkminiony
        // - zapisywanie nie zostalo rozkminione
        // - malo pisania, ale trzeba to obgadac
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        Date date;
        
        if(jTextField6.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "Wpisz datę.", "Błąd!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        try {
            date = format.parse(jTextField6.getText());
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, "Data nie jest poprawna.", "Błąd!", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        String nazwaSklepu = (String) jComboBox1.getSelectedItem();
        String wydatekNazwa = nazwaSklepu + " " + jTextField6.getText();
        int idKategoriiWydatku = sqldb.idKatWydatek(nazwaSklepu);
        Date dataWydatku = date;
        double wartoscWydatku = Float.parseFloat(jTextField7.getText());
        
        int wydatekID = sqldb.dodajWydatek(wydatekNazwa, idKategoriiWydatku, wartoscWydatku, new Timestamp(date.getTime()).toString());


        int sklepID = sqldb.idSklep(nazwaSklepu);
        LinkedList<Entry_rawDecorator> model = ((receiptReaderTool_jListProductModel)jList2.getModel()).prod;
        for(Entry_rawDecorator entry : model){
            entry.entry.date = date;
            
            int idKatProduktu = sqldb.idKatProdukt(entry.entry.category);
            if(idKatProduktu == 0)
                continue;
            
            int idProduktu = sqldb.dodajProdukt(entry.entry.name, sklepID, idKatProduktu, entry.entry.price,HomeBudgetApp.getIdbudzetu());
            if(idProduktu == 0)
                continue;
            sqldb.dodajProduktDoWydatku(wydatekID, idProduktu, entry.entry.quantity, entry.entry.price*entry.entry.quantity);
        }
        sqldb.dodajBudzetWydatek(HomeBudgetApp.getIdbudzetu(), wydatekID);
        sqldb.close();
        this.dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new receiptReaderTool(0).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    // End of variables declaration//GEN-END:variables
}
